package com.ymatou.iapi.productmanager.testcase.old;
import com.ymatou.iapi.productmanager.parameter.ActivityTabProductCountDto;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.ActivityProductStartSaleCall;
import com.ymatou.iapi.productmanager.service.ActivityProductSuspendSaleCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.*;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.LiveProductsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.compare.AssertHelper;

import org.json.JSONObject;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Ignore
public class Ts_ActivityProductStartSale {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播商品开始销售");
		intergratedProductMapper.updateProductoffSale(sellerId);
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new ActivityProductStartSaleCall().getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end(); 
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_ActivityProductStartSale_001() throws Exception {
		Logger.start(true,"直播商品开始销售");
		try{
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//List<String> productIds = ProductFactory.getProductWithExistStartLive(sellerId, sellerName, liveId, 5).stream().map(x->x.getProductId()).collect(Collectors.toList());
			List<String> productIds = ProductManagerCallServiceV2.getListProductId(sellerId,liveId,5);
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			ProductManagerCallServiceV2.addNewProductOnSale(addProdReq);
			//suspend
			new ActivityProductSuspendSaleCall().call(sellerId, liveId, productIds);
					
			JSONObject startSaleBean = new ActivityProductStartSaleCall().call(sellerId, liveId, productIds.subList(1,3));
				
			checkProductStartSale(startSaleBean,sellerId, liveId,productIds.subList(1,3),5,2);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_ActivityProductStartSale_002() {
		Logger.start(true,"商品列表中有不属于当前用户的商品");
		try{
			//2 create new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//List<String> productIds = ProductFactory.getProductWithExistStartLive(sellerId, sellerName, liveId, 3).stream().map(x->x.getProductId()).collect(Collectors.toList());
			List<String> productIds = ProductManagerCallServiceV2.getListProductId(sellerId,liveId,5);

			new ActivityProductSuspendSaleCall().call(sellerId, liveId, productIds);
			
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			String productId = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			productIds.add(productId);
			JSONObject startSaleBean = new ActivityProductStartSaleCall().call(sellerId, liveId, productIds);
			
			Logger.verifyEquals( 201,startSaleBean.getInt("Code"), "响应报文-返回码");
			Logger.verifyEquals( true,startSaleBean.getString("Msg").contains("不属于"), "响应报文-Msg");
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ActivityProductStartSale_003() {
		Logger.start(true,"删除商品开始销售");
		try{
			//2 create new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
		//	List<String> productIds = ProductFactory.getProductWithExistStartLive(sellerId, sellerName, liveId, 3).stream().map(x->x.getProductId()).collect(Collectors.toList());
			List<String> productIds = ProductManagerCallServiceV2.getListProductId(sellerId,liveId,5);
			new ActivityProductSuspendSaleCall().call(sellerId, liveId, productIds);
			
			SellerProductCallService.batchDeleteProduct(sellerId, productIds);
			
			JSONObject startSaleBean = new ActivityProductStartSaleCall().call(sellerId, liveId, productIds);
			
			Logger.verifyEquals( 201,startSaleBean.getInt("Code"), "响应报文-返回码");
			Logger.verifyEquals( true,startSaleBean.getString("Msg").contains("没有符合上架条件的商品"), "响应报文-Msg");
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ActivityProductStartSale_004() {
		Logger.start(true,"部分售罄库存商品在直播中开始销售");
		try{
			//2 create new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
		//	List<String> productIds = ProductFactory.getProductWithExistStartLive(sellerId, sellerName, liveId, 3).stream().map(x->x.getProductId()).collect(Collectors.toList());
			List<String> productIds = ProductManagerCallServiceV2.getListProductId(sellerId,liveId,3);
			new ActivityProductSuspendSaleCall().call(sellerId, liveId, productIds);
			
			
			
			SellerProductCallService.updateAllStockNumAs0(sellerId, productIds.get(0));
			JSONObject startSaleBean = new ActivityProductStartSaleCall().call(sellerId, liveId, productIds);

			checkProductStartSale(startSaleBean,sellerId, liveId,productIds.subList(1,3),3,2);
			
			Logger.verifyEquals(true, startSaleBean.toString().contains("不能上架"), "商品库存为0，不能上架");
			int status = (Integer)intergratedProductMapper.selectLiveProdByProdIdAndLiveId(productIds.get(0),liveId ).get(0).get("iSellStatus");
			Logger.verifyEquals(2, status, "商品库存为0，不能上架");
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ActivityProductStartSale_005() {
		Logger.start(true,"全部售罄库存商品在直播中开始销售");
		try{
			//2 create new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
		//	List<String> productIds = ProductFactory.getProductWithExistStartLive(sellerId, sellerName, liveId, 3).stream().map(x->x.getProductId()).collect(Collectors.toList());
			List<String> productIds = ProductManagerCallServiceV2.getListProductId(sellerId,liveId,5);
			new ActivityProductSuspendSaleCall().call(sellerId, liveId, productIds);
			
			
			
			productIds.forEach(x->{
				try {
					SellerProductCallService.updateAllStockNumAs0(sellerId, x);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			});
			JSONObject startSaleBean = new ActivityProductStartSaleCall().call(sellerId, liveId, productIds);


			productIds.forEach(x->{
				Logger.verifyEquals(true, startSaleBean.toString().contains("不能上架"), "商品库存为0，不能上架");
				int status = (Integer)intergratedProductMapper.selectLiveProdByProdIdAndLiveId(x,liveId ).get(0).get("iSellStatus");
				Logger.verifyEquals(2, status, "商品库存为0，不能上架");
			});
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	public static void checkProductStartSale(JSONObject ret,int sellerId,int liveId,List<String> productList,int tgtProdCount,int tgtputAwayCount) throws Exception{
		Logger.verifyEquals(200,ret.getInt("Code"), "响应报文-返回码");		
		ActivityTabProductCountDto tgtActivityTabProductCountDto = ProductManagerCallService.getActivityTabProductCountDto(sellerId,liveId);
		ActivityTabProductCountDto actActivityTabProductCountDto = ProductManagerCallService.getBeanFromJson(ret.getJSONObject("Data").getJSONObject("TabCount"), ActivityTabProductCountDto.class);
		
		AssertHelper.assertResultEqual(tgtActivityTabProductCountDto, actActivityTabProductCountDto, "TabCount检查");
		
		List<Map<String,Object>> liveProdInSql = intergratedProductMapper.selectLiveProdLiveId(liveId);
		int prodCount = liveProdInSql.stream().filter(x->((Integer)x.get("iAction"))==0).collect(Collectors.toList()).size();
		int putAwayCount = liveProdInSql.stream().filter(x->((Integer)x.get("iAction"))==0&&((Integer)x.get("iSellStatus"))==1).collect(Collectors.toList()).size();
		
		Logger.verifyEquals(tgtProdCount, prodCount, "productInLive product count");
		Logger.verifyEquals(tgtputAwayCount,putAwayCount, "productInLive put away count");
		
		for(String productId:productList){
			List<Map<String, Object>> liveInProductBean = intergratedProductMapper.selectLiveProdByProdIdAndLiveId(productId, liveId);
			
			Logger.verifyEquals(1, liveInProductBean.size(), "productInLive should be as 1");
			Logger.verifyEquals(1,(Integer)liveInProductBean.get(0).get("iSellStatus"), "isellerstatus should be 1");
			Logger.verifyEquals(0,(Integer)liveInProductBean.get(0).get("iIsTop"), "istop should be 0");
			
			//product monogo 检查
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Map<String, Object> map = new LiveProductsWapper().getByProductId(productId);
				Map<String, Object> tgtMap = MapUtil.hashMap("status", 1, "istop", false);
				AssertService.assertResultEqual(tgtMap, map,"Mongo-Liveproducts 开始销售检查");
			}
		}
	}
}
